﻿namespace PMS.Client.FrmOrgan
{
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Reflection;
    using System.Windows.Forms;

    public class FrmCheckDialog : Form
    {
        private OrganChangeLogBLL bll = new OrganChangeLogBLL();
        private Button btnCancel;
        private Button btnSave;
        private Button btnUnpass;
        private TextBox CheckRemark;
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private OrganChangeLog log = null;
        private int LogID = 0;
        private OrganBLL organBLL = new OrganBLL();
        private TextBox txtOrganName;

        public FrmCheckDialog(int LogID, string OrganName)
        {
            this.InitializeComponent();
            this.LogID = LogID;
            this.txtOrganName.Text = OrganName;
            this.log = this.bll.GetModel(LogID);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.bll.Check(this.LogID, "审核通过", this.CheckRemark.Text))
            {
                string logType = this.log.LogType;
                if (logType != null)
                {
                    if (!(logType == "新增"))
                    {
                        if (logType == "修改")
                        {
                            this.OrganUpdate();
                        }
                        else if (!(logType == "移动"))
                        {
                            if (logType == "删除")
                            {
                                this.OrganDelete();
                            }
                            else if ((logType == "撤消") || (logType == "恢复"))
                            {
                            }
                        }
                    }
                    else
                    {
                        this.OrganAdd();
                    }
                }
            }
        }

        private void btnUnpass_Click(object sender, EventArgs e)
        {
            if (this.bll.Check(this.LogID, "审核未通过", this.txtOrganName.Text))
            {
                base.DialogResult = DialogResult.OK;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmCheckDialog));
            this.txtOrganName = new TextBox();
            this.label1 = new Label();
            this.CheckRemark = new TextBox();
            this.label2 = new Label();
            this.btnCancel = new Button();
            this.btnSave = new Button();
            this.btnUnpass = new Button();
            base.SuspendLayout();
            this.txtOrganName.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.txtOrganName.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtOrganName.BorderStyle = BorderStyle.FixedSingle;
            this.txtOrganName.Location = new Point(0x5b, 0x23);
            this.txtOrganName.Name = "txtOrganName";
            this.txtOrganName.ReadOnly = true;
            this.txtOrganName.Size = new Size(0x11d, 0x15);
            this.txtOrganName.TabIndex = 3;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x20, 0x26);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x35, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "机构名称";
            this.CheckRemark.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.CheckRemark.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.CheckRemark.BorderStyle = BorderStyle.FixedSingle;
            this.CheckRemark.Location = new Point(0x5b, 0x4d);
            this.CheckRemark.Multiline = true;
            this.CheckRemark.Name = "CheckRemark";
            this.CheckRemark.Size = new Size(0x11d, 0x5d);
            this.CheckRemark.TabIndex = 5;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x20, 80);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x35, 12);
            this.label2.TabIndex = 4;
            this.label2.Text = "审核备注";
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x12d, 0xcd);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 7;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(0x5b, 0xcd);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 6;
            this.btnSave.Text = "通过";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.btnUnpass.BackColor = Color.White;
            this.btnUnpass.FlatStyle = FlatStyle.Popup;
            this.btnUnpass.Location = new Point(0xc5, 0xcd);
            this.btnUnpass.Name = "btnUnpass";
            this.btnUnpass.Size = new Size(0x4b, 0x17);
            this.btnUnpass.TabIndex = 8;
            this.btnUnpass.Text = "不通过";
            this.btnUnpass.UseVisualStyleBackColor = false;
            this.btnUnpass.Click += new EventHandler(this.btnUnpass_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x1b3, 0x103);
            base.Controls.Add(this.btnUnpass);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.CheckRemark);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.txtOrganName);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmCheckDialog";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "机构审核";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void OrganAdd()
        {
            Organ organ = new Organ();
            List<PropertyInfo> source = this.log.GetType().GetProperties().ToList<PropertyInfo>();
            Func<PropertyInfo, bool> predicate = null;
            foreach (PropertyInfo p in organ.GetType().GetProperties())
            {
                if (predicate == null)
                {
                    predicate = f => f.Name == p.Name;
                }
                p.SetValue(organ, source.FirstOrDefault<PropertyInfo>(predicate).GetValue(this.log, null), null);
            }
            int organID = this.organBLL.Add(organ);
            if (organID > 0)
            {
                this.bll.UpdateOrganID(this.log.LogID, organID);
                base.DialogResult = DialogResult.OK;
            }
            else
            {
                this.bll.Check(this.LogID, "未审核", "");
            }
        }

        private void OrganDelete()
        {
            if (this.organBLL.Delete(this.log.OrganID))
            {
                base.DialogResult = DialogResult.OK;
            }
            else
            {
                this.bll.Check(this.LogID, "未审核", "");
            }
        }

        private void OrganUpdate()
        {
            Organ model = this.organBLL.GetModel(this.log.OrganID);
            List<PropertyInfo> source = this.log.GetType().GetProperties().ToList<PropertyInfo>();
            Func<PropertyInfo, bool> predicate = null;
            foreach (PropertyInfo p in model.GetType().GetProperties())
            {
                if (predicate == null)
                {
                    predicate = f => f.Name == p.Name;
                }
                object obj2 = source.FirstOrDefault<PropertyInfo>(predicate).GetValue(this.log, null);
                if ((obj2 != null) && (obj2.ToString() != ""))
                {
                    p.SetValue(model, obj2, null);
                }
            }
            if (this.organBLL.Update(model))
            {
                SyslogBLL.Write("机构审核", "机构审核成功");
                base.DialogResult = DialogResult.OK;
            }
            else
            {
                this.bll.Check(this.LogID, "未审核", "");
            }
        }
    }
}

